﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace InstallCheckVariable
{
    public partial class Form1 : Form
    {
        RegistryKey regRootKey;
        RegistryKey regSubKey;
        string installLocation = string.Empty;
        string displayVersion = string.Empty;
        string uiPath = "";
        byte[] uiPathDll;
        string strRegPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\UiPath";

        Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            //读取资源
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("InstallCheckVariable.CheckVariable.dll"))
            {
                uiPathDll = new byte[stream.Length];
                stream.Read(uiPathDll, 0, (int)stream.Length);
                return Assembly.Load(uiPathDll);//加载资源文件中的dll,代替加载失败的程序集
            }
        }

        public Form1()
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            InitializeComponent();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            regRootKey = Registry.CurrentUser;
            regSubKey = regRootKey.OpenSubKey(strRegPath);
            // InstallLocation
            installLocation = regSubKey.GetValue("InstallLocation").ToString();
            // DisplayVersion
            displayVersion = regSubKey.GetValue("DisplayVersion").ToString();
            uiPath = installLocation + @"\app-" + displayVersion + @"\Rules";
            this.txt_uipath_path.Text = uiPath;
            regSubKey.Close();
            regRootKey.Close();
        }

        private void btn_Select_uipath_Click(object sender, EventArgs e)
        {
            this.folderDialog_uipath.SelectedPath = uiPath;
            this.folderDialog_uipath.ShowDialog();
            uiPath = this.folderDialog_uipath.SelectedPath;
            this.txt_uipath_path.Text = this.folderDialog_uipath.SelectedPath;
        }

        private void btn_Install_Click(object sender, EventArgs e)
        {
            if (this.btn_Install.Text != "Close")
            {
                try
                {
                    FileStream fs = new FileStream(uiPath + @"\CheckVariable.dll", FileMode.Create);
                    fs.Write(uiPathDll, 0, uiPathDll.Length);
                    fs.Dispose();

                    this.lb_uipath.Visible = true;
                    this.progressBar_uipath.Visible = true;
                    this.btn_Cancel.Enabled = false;
                    this.btn_Install.Text = "Close";
                }
                catch (Exception)
                {
                    MessageBox.Show("UiPath.Studio 已经打开项目，请关闭项目再安装！(HOME -> Close)");
                }
            }
            else
            {
                Application.Exit();
            }
        }
    }
}
